package cn.wzvtc.soft14;


import cn.wzvtc.soft14.Service.*;
import cn.wzvtc.soft14.domian.publish;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by linji on 2016/3/4.
        */

@RestController
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
@CrossOrigin(origins = {},methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
public class Contorl extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception{
        SpringApplication.run(Contorl.class, args);
    }

    @Autowired
    private StudentInfoService studentInfoService;

    @RequestMapping("/stuInfo")
    //@ResponseBody
    Object stuInfo(String studentId)
    {
        return this.studentInfoService.getStudentInfo(studentId);
    }

    @RequestMapping("/stu")
    List stu(){
        return this.studentInfoService.select();
    }

    @Autowired
    private ProPeopleService proPeopleService;

    @RequestMapping("/stuProjectPeopleInfo")
    //@ResponseBody
    Object stuProjectPeopleInfo(String studentId)
    {
        return this.proPeopleService.getStudentInfoTwo(studentId);
    }

    @RequestMapping("/stuProjectPeopleInfoSize")
        //@ResponseBody
    Object stuProjectPeopleInfoSize(String studentId)

    {
        int result = this.proPeopleService.getStudentCount(studentId);
        String json="{\"count\":"+result+"}";
        return json;
    }

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/stuProjectInfo")
    @ResponseBody
    Object stuProjectInfo(@RequestParam("projectId") String projectId)
    {
        return this.projectService.getProjectInfo(projectId);
    }

    @Autowired
    private PublishService publishService;

    @RequestMapping("/publishInfo")
    @ResponseBody
    Object publishInfo(String studentId)
    {
        return this.publishService.publishSelect(studentId);
    }

    @RequestMapping("/publishInfoSize")
    @ResponseBody
    Object publishInfoSize(String studentId)
    {
        int result = this.publishService.publishCount(studentId);
        Map p=new HashMap();
        p.get(result);
        List a=new ArrayList();
        a.add(p);
        String json="{\"count\":"+result+"}";
        return a;
    }

    @RequestMapping("/publishsave")
    @ResponseBody
    Object publishsave(@RequestParam("studentId") String studentId,
                       @RequestParam("zxType") int zxType,
                       /*@RequestParam("end") boolean end,*/
                       @RequestParam("type") int type)

    {
        publish pu=new publish();
        pu.setId(UUID.randomUUID().toString());
        pu.setProjectId("10002");
        pu.setReleaseId("1002");
        java.util.Date date = new java.util.Date();
        DateFormat timeTwo = new SimpleDateFormat("yyyy");
        String Time = timeTwo.format(date);
        pu.setSemester(Time);
        pu.setStudentId(studentId);
       // DateFormat time = DateFormat.getDateTimeInstance();
        DateFormat time = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String nowTime = time.format(date);
        pu.setTime(nowTime);
        pu.setType(type);
        pu.setWeek("18");
        pu.setZc(18);
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, 3);
        String endTime = time.format(ca.getTime());
        pu.setEndTime(endTime);
        pu.setEnd(true);
        pu.setZxType(zxType);
        return this.publishService.savepublish(pu);
    }

    @RequestMapping("/publishDelete")
    @ResponseBody
    Object publishDelete(@RequestParam("studentId") String studentId)
    {
        publish pu=new publish();
        pu.setStudentId(studentId);
        return this.publishService.deleteUser(pu);
    }

    @Autowired
    private UserService userService;

    @RequestMapping("/loginUser")
    @ResponseBody
    Object loginUser(@RequestParam("name") String name,
                     @RequestParam("pass") String pass)
    {
        return this.userService.isExist(name,pass);
    }

    @RequestMapping("/upload")
    @ResponseBody
    Object upload()
    {
        String ACCESS_KEY = "hKR49KfLD7Ajip_66zBrc-7ZWWwr7AezLHvl7Vwx";
        String SECRET_KEY = "bKtZK8FQCttIZo_4CaBVbIEm6HTmZH2B_5Uo7qQN";

        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        return auth.uploadToken("hudong");
    }
}
